package com.zmn.oms.task.job.ins;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.task.ins.InsOrderSyncBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：按单保险同步任务
 *
 * @author lhl
 * @date 2020/06/17 19:39
 */
@LTS
@Slf4j
public class InsOrderSyncJob {

    @Resource
    TaskManager taskManager;

    @Autowired
    private InsOrderSyncBService insOrderSyncBService;

    /**
     * 每隔1分钟，同步一次按单保险数据
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_SYNC_INS_ORDER);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_SYNC_INS_ORDER);
        job.setCronExpression("0 /1 * * * ?");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#oms#sync_insOrder_job# job submit fail...");
        }
    }
    /**
     * 同步按单保险
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_SYNC_INS_ORDER)
    public Result runWorkJob(Job job) {
        try {
            log.info("按单保险同步-Start");
            insOrderSyncBService.execSycn();
            log.info("按单保险同步-End");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
    /**
     * 检查二次同步保险信息是否出单
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_RESYNC_INS_ORDER_CHECK)
    public Result runCheckJob(Job job) {
        try {
            log.info("检查二次同步保险信息是否出单-Start");
            String insMasterIds = job.getParam("insMasterIds");
            if (StringUtil.isNotBlank(insMasterIds)) {
                insOrderSyncBService.checkResyncResult(insMasterIds);
            }
            log.info("检查二次同步保险信息是否出单-End");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
